package com.laozhao.wintersystem;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JMenuItem;

import org.jdesktop.swingx.JXTaskPane;
import org.jdesktop.swingx.JXTaskPaneContainer;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.laozhao.component.DefaultTaskItem;

public class WTasksModel {

	private final String ELEMENT_TASKS_TASK = "task";
	private final String ELEMENT_TASKS_TASKITEM = "taskitem";

	private final String ATTLIST_TASK_NAME = "name";
	private final String ATTLIST_TASK_ICON = "icon";
	private final String ATTLIST_TASK_TIP = "tip";

	private final String ATTLIST_TASKITEM_NAME = "name";
	private final String ATTLIST_TASKITEM_ICON = "icon";
	private final String ATTLIST_TASKITEM_TIP = "tip";
	private final String ATTLIST_TASKITEM_LISTENER = "listener";
	private final String ATTLIST_TASKITEM_COMMAND = "command";

	private JXTaskPaneContainer container = new JXTaskPaneContainer();
	private Map<DefaultTaskItem, String> itemsMap = new HashMap<DefaultTaskItem, String>();

	String tmpName;
	Icon tmpIcon;
	String tmpTip;
	String tmpListener;
	String tmpCommand;

	public WTasksModel(Node node) {
		load(node);
	}

	public Component getTaskContainer() {
		return container;
	}

	public void addListenerModel(WListenersModel lModel) {
		if (!itemsMap.isEmpty()) {
			for (Map.Entry<DefaultTaskItem, String> entry : itemsMap.entrySet()) {
				entry.getKey().addActionListener(lModel.getListener(entry.getValue()));
			}
		}
	}
	
	public void addActionListener(ActionListener l){
		if (!itemsMap.isEmpty()) {
			for (DefaultTaskItem menuItem : itemsMap.keySet()) {
				menuItem.addActionListener(l);
			}
		}
	}
	
	private void load(Node node) {
		if (node != null) {
			// <!ELEMENT menubar(nenu+,nenuitem?)>
			NodeList menuNodes = node.getChildNodes();
			for (int k = 0; k < menuNodes.getLength(); k++) {
				Node menu = menuNodes.item(k);
				if (menu.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
						&& node.getNodeName().equalsIgnoreCase(
								ELEMENT_TASKS_TASK)) {
					tmpName = WSTools.getStringAttribute(menu,
							ATTLIST_TASK_NAME);
					tmpIcon = WSTools.getIconAttribute(menu, ATTLIST_TASK_ICON);
					tmpTip = WSTools.getStringAttribute(menu, ATTLIST_TASK_TIP);
					JXTaskPane taskPane = new JXTaskPane(tmpName, tmpIcon);
					taskPane.setToolTipText(tmpTip);
					createTaskItem(taskPane, menu);
					container.add(taskPane);
				}
			}
		}
	}

	private void createTaskItem(JXTaskPane taskPane, Node node) {
		if (node != null) {
			NodeList taskItems = node.getChildNodes();
			for (int k = 0; k < taskItems.getLength(); k++) {
				Node taskItem = taskItems.item(k);
				if (taskItem.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
						&& taskItem.getNodeName().equalsIgnoreCase(
								ELEMENT_TASKS_TASKITEM)) {
					tmpName = WSTools.getStringAttribute(taskItem,
							ATTLIST_TASKITEM_NAME);
					tmpIcon = WSTools.getIconAttribute(taskItem,
							ATTLIST_TASKITEM_ICON);
					tmpTip = WSTools.getStringAttribute(taskItem,
							ATTLIST_TASKITEM_TIP);
					tmpListener = WSTools.getStringAttribute(taskItem,
							ATTLIST_TASKITEM_LISTENER);
					tmpCommand = WSTools.getStringAttribute(taskItem,
							ATTLIST_TASKITEM_COMMAND);
					DefaultTaskItem jtaskItem = new DefaultTaskItem(tmpName,
							tmpIcon, tmpTip);

					// menuItem.setText(text);
					jtaskItem.setActionCommand(tmpCommand);
					// menuItem.
					taskPane.add(jtaskItem);
					if (tmpListener == null || "".equals(tmpListener)) {
						tmpListener = WListenersModel.DEFAULT_TASK_LISTENER_ID;
					}
					itemsMap.put(jtaskItem, tmpListener);
				}
			}
		}

	}

}
